//
//  WBVisitorView.swift
//  CustomWeiBo
//
//  Created by laiqiang on 21/5/11.
//  Copyright © 2021年 laiqiang. All rights reserved.
//

import UIKit

class WBVisitorView: UIView {
    
    //注册
    lazy var registerbtn = UIButton.init()
    //登录
    lazy var loginbtn = UIButton.init()
    
    var viewnInfo: [String:String]?
        
    var visitorinfo: [String:String]?{
        get{
        
            return self.viewnInfo!;
        }
        set(visitorinfo){
        
            self.viewnInfo = visitorinfo;//字典信息
            guard  let imgName = visitorinfo?["imgName"],
                let message = visitorinfo?["message"]
                else {
                    return
            }
            
            tipLable.text = message
            if imgName == "" {
                //首页 imgName ＝＝ “”
                setAnimation()
                return
            }
            
            iconView.image = UIImage(named:imgName)
        }
    }
    
    
     //MARK:私有控件 －－ 重要的属性在上面
    override init(frame:CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    //MARK:设置访客视图信息
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //旋转动画
    private func setAnimation (){
        
        let anim = CABasicAnimation(keyPath:"transform.rotation")
        anim.toValue = 2 * M_PI
        anim.repeatCount = MAXFLOAT
        anim.duration = 15
        
        //停止之后不删除
        anim.isRemovedOnCompletion = false
        
        //添加到图层
        iconView.layer.add(anim, forKey: nil)
    }
    
    //MARK:私有控件 －－ 重要的属性在上面，不重要的在下面
    
    //图片
    fileprivate lazy var iconView = UIImageView.init(image: UIImage(named: "visitor_home"))
    //房子
    fileprivate lazy var headView = UIImageView.init(image: UIImage(named: "visitor_head"))
    //提示文本,懒加载属性调用uikit。控件的指定构造函数，需要制定类型
    fileprivate lazy var tipLable = UILabel.init()


//    fileprivate lazy var maskIconView = UIButton.init()
}

extension WBVisitorView{

    func setupUI() {
        
        backgroundColor = UIColor.colorWithaHex(hexStr:"0xEDEDED")
        
//        maskIconView.backgroundColor = UIColor.red
        
        tipLable.text = "关注一些人，回这里看看有什么惊喜关注一些人会这里看看有什么惊喜"
        tipLable.textColor = UIColor.darkGray
        tipLable.font = UIFont.systemFont(ofSize: 14)
        tipLable.numberOfLines = 0
        tipLable.textAlignment = .center
        tipLable.sizeToFit()
        
        //注册
        registerbtn.setTitle("注册", for: .normal)
        registerbtn.setTitleColor(UIColor().WBPinkColor, for: .normal)
        registerbtn.setTitleColor(UIColor.black, for: .highlighted)
//        registerbtn.setBackgroundImage(UIImage(named: "visitor_home"), for: .normal)
        registerbtn.layer.cornerRadius = 5.0
        registerbtn.layer.masksToBounds = true
        registerbtn.layer.borderWidth = 1.0
        registerbtn.layer.borderColor = UIColor.lightGray.cgColor
        registerbtn.sizeToFit()
        
        
        //登录
        loginbtn.setTitle("登录", for: .normal)
        loginbtn.setTitleColor(UIColor.darkGray, for: .normal)
        loginbtn.setTitleColor(UIColor.black, for: .highlighted)
        //        registerbtn.setBackgroundImage(UIImage(named: "visitor_home"), for: .normal)
        loginbtn.layer.cornerRadius = 5.0
        loginbtn.layer.masksToBounds = true
        loginbtn.layer.borderWidth = 1.0
        loginbtn.layer.borderColor = UIColor.lightGray.cgColor
        loginbtn.sizeToFit()
        
//        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(tipLable)
        addSubview(registerbtn)
        addSubview(loginbtn)
        
        //取消代码的autoresizing 自适应（因为和storyboard的autoyout 不能共存的）
        for v in subviews {
            v.translatesAutoresizingMaskIntoConstraints = false
        }
        
        //房子自动布局
        //参照toItem: self 自己
        //multiplier 乘数 （偏移倍数 |ˈmʌl tɪ plaɪə| 忙 特 扑 来 耳）
        //constant 乘数 （偏移倍数 持续的 ，微调长度）
        
        let margin:CGFloat = 20
        
        
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView,
                                         attribute: .centerY,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerY,
                                         multiplier: 1.0,
                                         constant: 0))
        
        
        //标签自动布局
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .centerX,
                                         relatedBy: .equal,
                                         toItem: self,
                                         attribute: .centerX,
                                         multiplier: 1.0,
                                         constant: 0))
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: iconView,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        
        addConstraint(NSLayoutConstraint(item: tipLable,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 236))
        
        //注册
        addConstraint(NSLayoutConstraint(item: registerbtn,
                                         attribute: .left,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .left,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: registerbtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        
        addConstraint(NSLayoutConstraint(item: registerbtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: nil,
                                         attribute: .notAnAttribute,
                                         multiplier: 1.0,
                                         constant: 100))
        //登录
        addConstraint(NSLayoutConstraint(item: loginbtn,
                                         attribute: .right,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .right,
                                         multiplier: 1.0,
                                         constant: 0))
        
        addConstraint(NSLayoutConstraint(item: loginbtn,
                                         attribute: .top,
                                         relatedBy: .equal,
                                         toItem: tipLable,
                                         attribute: .bottom,
                                         multiplier: 1.0,
                                         constant: margin))
        
        addConstraint(NSLayoutConstraint(item: loginbtn,
                                         attribute: .width,
                                         relatedBy: .equal,
                                         toItem: registerbtn,
                                         attribute: .width,
                                         multiplier: 1.0,
                                         constant: 0))
//    //VFL 线性布局
//        let viewdic = ["maskIconView":maskIconView]//                       "registerbtn":registerbtn
//        
//        //水平布局：-0- 中的0表示间距
//        let visualH = "H:|-0-[maskIconView]-0-|"
//        //垂直布局
////        let visualV = "V:|-0-[maskIconView]-0-|"
//        let visualV = "V:|-0-[maskIconView]-0-|"
//        
//        //水平布局
//        addConstraints(NSLayoutConstraint.constraints(
//            withVisualFormat: visualH,
//            options: [],
//            metrics: nil,
//            views: viewdic))
//        //垂直布局
//        addConstraints(NSLayoutConstraint.constraints(
//            withVisualFormat: visualV,
//            options: [],
//            metrics: nil,
//            views: viewdic))
        
//        let visualV = "V:|-0-[maskIconView]-[spacing]-[registerbtn]|"
//        let metrics = ["spacing":-35] 将metrics的spacing的值，映射到visualV中
        
//        addConstraints(NSLayoutConstraint.constraints(
//            withVisualFormat: visualV,
//            options: [],
//            metrics: metrics,
//            views: viewdic))

    }
}
